package com.qst.rongxiaotong.controller;

import com.qst.rongxiaotong.common.Result;
import com.qst.rongxiaotong.common.StatusCode;
import com.qst.rongxiaotong.context.BaseContext;
import com.qst.rongxiaotong.pojo.*;
import com.qst.rongxiaotong.service.PurchaseDetailService;
import com.qst.rongxiaotong.service.PurchaseService;
import com.qst.rongxiaotong.service.SellPurchaseService;
import com.qst.rongxiaotong.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "购物车模块管理")
@RequestMapping("/cart")
@CrossOrigin
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;
    @Autowired
    PurchaseService purchaseService;
    @Autowired
    PurchaseDetailService purchaseDetailService;
    @Autowired
    SellPurchaseService sellPurchaseService;

    @ApiOperation("/添加购物车信息")
    @PostMapping("/add/{id}")
    public Result add(@PathVariable("id")Integer id){

        String name = BaseContext.getCurrentuserName();
        ShoppingCart shoppingcart =new ShoppingCart()
                .setOwnName(name)
                .setOrderId(id)
                .setCount(1)
                .setCreateTime(new Date())
                .setUpdateTime(new Date());
        ShoppingCart shoppingCart = shoppingCartService.add(shoppingcart);
        return new Result<>(true, StatusCode.OK,"添加成功",shoppingCart);
    }

    @ApiOperation("/删除购物车信息")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id")Integer id){
        int row  = shoppingCartService.delete(id);
        if (row!=1){
            return new Result<>(true, StatusCode.OK,"删除成功");
        }
        return new Result<>(false, StatusCode.ERROR,"删除成功");
    }
    @ApiOperation("/更新购物车数量")
    @PutMapping("/update/{id}/{count}")
    public Result update(@PathVariable("id")Integer id,@PathVariable("count")Integer count){
        ShoppingCart shoppingcart =new ShoppingCart()
                .setShoppingId(id)
                .setCount(count)
                .setUpdateTime(new Date());
        shoppingCartService.update(shoppingcart);
        return new Result<>(true, StatusCode.OK,"更新成功");
    }

    @ApiOperation("/显示购物车里的所有商品信息")
    @GetMapping("/show")
    public Result<List<ShoppingModel>> show(){
        List<ShoppingModel> shoppingModel = shoppingCartService.selectByUsername();
        return new Result<List<ShoppingModel>>(true, StatusCode.OK, "查询成功", shoppingModel);
    }

    @ApiOperation("/生成订单")
    @PostMapping("/commitOrder/{addId}/{tMoney}")
    public Result commitOrder(@PathVariable("addId") Integer addId,
                              @PathVariable("tMoney") String tMoney,
                              @RequestBody List<ShoppingModel> shoppingModelList ){
        //添加购买人的详细订单信息
        String username = BaseContext.getCurrentuserName();
        Purchase purchase = new Purchase()
                .setPurchaseType(1)
                .setOwnName(username)
                .setAddress(String.valueOf(addId))
                .setPurchaseStatus(1)
                .setTotalPrice(new BigDecimal(tMoney))
                .setCreateTime(new Date())
                .setUpdateTime(new Date());
        purchaseService.add(purchase);

        Purchase purchaseGetId = purchaseService.selectByName(username);
        Integer purchaseId = purchaseGetId.getPurchaseId();
        if(shoppingModelList!=null&&shoppingModelList.size()>0){
            for(int i=0;i<shoppingModelList.size();i++){
                ShoppingModel shoppingModel = shoppingModelList.get(i);
                PurchaseDetail purchaseDetail = new PurchaseDetail()
                        .setCount(shoppingModel.getCount())
                        .setOrderId(shoppingModel.getOrderId())
                        .setPurchaseId(purchaseId)
                        .setSumPrice(new BigDecimal(Double.parseDouble(shoppingModel.getPrice())*shoppingModel.getCount()))
                        .setUninPrice(new BigDecimal(shoppingModel.getPrice()));
                purchaseDetailService.add(purchaseDetail);
                //卖出订单信息
                SellPurchase sellPurchase = new SellPurchase()
                        .setPurchaseId(shoppingModel.getShoppingId())
                        .setAddress(addId.toString())
                        .setOwnName(shoppingModel.getOwnName())
                        .setOrderId(shoppingModel.getOrderId())
                        .setPurchaseType(2)
                        .setPurchaseStatus(1)
                        .setCreateTime(new Date())
                        .setUpdateTime(new Date())
                        .setUninPricee(new BigDecimal(shoppingModel.getPrice()))
                        .setSumPrice(new BigDecimal(Double.parseDouble(shoppingModel.getPrice())* shoppingModel.getCount()));
                sellPurchaseService.add(sellPurchase);
                shoppingCartService.delete(shoppingModel.getShoppingId());
            }
        }
        return new Result<>(true, StatusCode.OK,"提交成功");
    }
}
